__author__ = 'Shoohy'

import json
import urlparse
import webapp2
from model_child import Child
from model_att import Att
from model_PC import PC
from model_gcm import GCM
from model_KG import Kindergarten
from model_message import Message

class AndroidHandler(webapp2.RequestHandler):
    #URL is of form 'http://apphost/action?query'
    def get(self, action):
        URL = self.request.url
        parseURL = urlparse.urlparse(URL)
     #   action = parseURL.path
        query = parseURL.query
        j = json.dumps(dict(urlparse.parse_qsl(query)))
        returnVal = None


        ## RETRIEVING INFORMATION ##
        if action == 'getChildren':          #get parent's children
            returnVal = Child.create_children_dictionary(PC.get_children_by_parent_mail_address(query))
        elif action == 'getChildInfo':         #get child information
            returnVal = Child.get_child_info(query)
        elif action == 'getChildAttendance':   #get child attendance
            returnVal = Att.get_att_of_child_by_child_id_and_date(j)  #j is of form 'child_id=1234&date=2013-11-19'
        elif action == 'getKGInfo':
            returnVal = Kindergarten.get_KG_info_by_KG_id(j)
        elif action == 'getKGidByChildId':
            returnVal = Child.get_KG_id_by_given_child_id_in_json(j)
        elif action == 'getKGInfoByKGid':
            returnVal = Kindergarten.get_KG_info_by_KG_id(j)
        elif action == 'addRegId':
            returnVal = GCM.create_new_register_id_by_parent_id(j)
        elif action == 'deleteRegId':
            returnVal = GCM.delete_reg_id_by_parent_email_address(j)
        elif action == 'getAllMsgByKG':
            returnVal = Message.get_all_public_messages_by_KG_id_for_android(j)
        elif action == 'getAllMsgByChild':
            returnVal = Message.get_all_private_messages_by_child_id_for_android(j)
        elif action == 'getRegIdByParentMail':
            returnVal = GCM.get_reg_id_by_parent_email_address_in_json(j)
        elif action == 'getChildrenIdsByKGid':
            returnVal = Kindergarten.get_all_children_ids_in_KG_by_json(j)
        elif action == 'sendMessageToTeacher':
            returnVal = Message.create_message_from_json(j)
        # TODO: delete at the end !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        elif action == 'testGCMByKG':
            returnVal = GCM.send_arriving_att_to_parents(j)
        elif action == 'testMSGByKG':
            returnVal = GCM.send_new_message_to_parents(j)


        return self.response.write(returnVal)
